package com.vineetmanohar.nitrogen;

import java.io.File;
import java.util.Collection;

import org.apache.commons.io.FileUtils;

/**
 * This is the high level class which provides a rich code generation utility.
 * Behind the scenes, it uses text generator, file generator and path generators
 * to accomplish the high goal of code generation.
 * 
 * In most cases, users will use only this class which in turn will call other
 * generators.
 * 
 * @author vineetmanohar
 */
public class CodeGenerator {
	/**
	 * 
	 * @param model
	 *            the file which will be used to construct the model needed for
	 *            code generation
	 * @param inputFile
	 *            the input template
	 * @param outputDir
	 *            the directory where the output should be written
	 */
	@SuppressWarnings("unchecked")
	public static void generate(File modelFile, final File inputBasedir,
			final File outputBasedir) {
		ObjectModel model = ModelFactory.parseModelFile(modelFile);
		CodeGenerationContext codeGenerationContext = CodeGenerationContext
				.createContextFromObjectModel(model);

		PathCollector pathCollector = new PathCollector() {
			public void collect(GeneratedPath generatedPath) {
				FileGenerator.generateFile(generatedPath, inputBasedir,
						outputBasedir);
			}
		};

		for (File file : (Collection<File>) FileUtils.listFiles(inputBasedir,
				null, true)) {
			String relativePath = NitrogenUtils.getRelativePath(file,
					inputBasedir);

			PathGenerator.generatePath(codeGenerationContext, relativePath,
					pathCollector);
		}
	}
}
